package com.stu.config;

/**
 * @author zhuzi
 * 2022/8/17 23:25
 * <a href="https://mp.weixin.qq.com/s/594-WxyNCi2rDgp8hJTdsw">统一返回</a>
 */
//@RestControllerAdvice(basePackages = {"com.stu"})
//public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
//
//    @Override
//    public boolean supports(MethodParameter returnType,Class<? extends HttpMessageConverter<?>> converterType) {
//        // response是ResultVo类型，或者注释了NotControllerResponseAdvice都不进行包装
//        return !returnType.getParameterType().isAssignableFrom(RespVo.class);
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object body,MethodParameter returnType,MediaType selectedContentType,Class<? extends HttpMessageConverter<?>> selectedConverterType,ServerHttpRequest request,ServerHttpResponse response) {
//        if (returnType.getGenericParameterType().equals(String.class)) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                // 将数据包装在ResultVo里后转换为json串进行返回
//                return objectMapper.writeValueAsString(new ResultVo(data));
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException(e.getMessage());
//            }
//        }
//        // 否则直接包装成ResultVo返回
//        return new ResultVo(data);
//    }
//}
